/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.d.ui.view.sort;

import com.d.lib.common.widget.TitleLayout;
import com.d.lib.ui.view.LogUtil;
import com.d.lib.ui.view.sort.SideBar;
import com.d.lib.ui.view.sort.SortBean;
import com.d.lib.ui.view.sort.SortUtil;
import com.d.ui.view.ResourceTable;
import java.util.ArrayList;
import java.util.List;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;

public class SortActivity extends AbilitySlice implements Component.ClickedListener {

  private DirectionalLayout llyt_tin;
  private Text tv_tin_letter;
  private ListContainer rv_list;
  private SideBar sb_sidebar;
  private SortUtil sortUtil;
  private PopupDialog popupDialog;
  private Text tvCenter;
  private Text tvNormal;

  @Override
  protected void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_activity_sort);

    bindView();
    initConfig();
  }

  private void bindView() {
    llyt_tin = (DirectionalLayout) findComponentById(ResourceTable.Id_llyt_tin);
    tv_tin_letter = (Text) findComponentById(ResourceTable.Id_tv_tin_letter);
    rv_list = (ListContainer) findComponentById(ResourceTable.Id_rv_list);
    sb_sidebar = (SideBar) findComponentById(ResourceTable.Id_sb_sidebar);

    TitleLayout titleLayout = (TitleLayout) findComponentById(ResourceTable.Id_tl_title);
    titleLayout.setBackClickListener(this::onClick);
    titleLayout.setMoreClickListener(this::onMoreClick);
  }

  private void onMoreClick(Component component) {
    LogUtil.debug("sort", " more ");
    Component mRootView =
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_popup_sort, null, true);
    tvNormal = (Text) mRootView.findComponentById(ResourceTable.Id_tv_normal);
    tvCenter = (Text) mRootView.findComponentById(ResourceTable.Id_tv_center);

    tvCenter.setClickedListener(new Component.ClickedListener() {
      @Override
      public void onClick(Component component) {
        sb_sidebar.setType(SideBar.TYPE_CENTER);
        sb_sidebar.reset(sortUtil.sortDatas(getDatas()));
        popupDialog.destroy();
      }
    });
    tvNormal.setClickedListener(new Component.ClickedListener() {
      @Override
      public void onClick(Component component) {
        sb_sidebar.setType(SideBar.TYPE_NORMAL);
        sb_sidebar.reset(sortUtil.sortDatas(getDatas()));
        popupDialog.destroy();
      }
    });
    popupDialog = new PopupDialog(getContext(), component);
    popupDialog.setCustomComponent(mRootView);
    popupDialog.setHasArrow(true);
    popupDialog.setArrowOffset(120);
    popupDialog.setMode(LayoutAlignment.LEFT | LayoutAlignment.BOTTOM);
    popupDialog.setBackColor(Color.BLACK);
    popupDialog.show();
  }

  private void initConfig() {
    List<SortBean> datas = getDatas();
    sortUtil = new SortUtil();
    rv_list.setLayoutManager(new DirectionalLayoutManager());
    rv_list.setItemProvider(new SortAdapter(this, datas, ResourceTable.Layout_adapter_sort));
    rv_list.addScrolledListener(new Component.ScrolledListener() {
      @Override
      public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
        sortUtil.onScrolled(rv_list, llyt_tin, tv_tin_letter);
      }
    });
    sb_sidebar.setType(SideBar.TYPE_CENTER);
    sb_sidebar.reset(sortUtil.sortDatas(datas));
    sb_sidebar.setOnLetterChangedListener(new SideBar.OnLetterChangedListener() {
      @Override
      public void onChange(int index, String c) {
        sortUtil.onChange(index, c, rv_list);
      }
    });
  }

  private List<SortBean> getDatas() {
    List<SortBean> hots = new ArrayList<>();
    hots.add(new SortBean("", "131"));
    hots.add(new SortBean("", "265B妹"));
    hots.add(new SortBean("", "265阿妹"));
    hots.add(new SortBean("", "867"));
    hots.add(new SortBean("", "496"));
    hots.add(new SortBean("", "00444"));
    hots.add(new SortBean("", "34475"));
    hots.add(new SortBean("", "阿妹"));
    hots.add(new SortBean("a", "阿郎"));
    hots.add(new SortBean("a", "陈奕迅"));
    hots.add(new SortBean("a", "周杰伦"));
    hots.add(new SortBean("c", "曾一鸣"));
    hots.add(new SortBean("c", "成龙"));
    hots.add(new SortBean("c", "王力宏"));
    hots.add(new SortBean("c", "汪峰"));
    hots.add(new SortBean("w", "王菲"));
    hots.add(new SortBean("n", "那英"));
    hots.add(new SortBean("z", "张伟"));
    hots.add(new SortBean("z", "张学友"));
    hots.add(new SortBean("l", "李德华"));
    hots.add(new SortBean("z", "郑源"));
    hots.add(new SortBean("b", "白水水"));
    hots.add(new SortBean("b", "白天不亮"));
    hots.add(new SortBean("c", "陈龙"));
    hots.add(new SortBean("c", "陈丽丽"));
    hots.add(new SortBean("h", "哈林"));
    hots.add(new SortBean("g", "高进"));
    hots.add(new SortBean("g", "高雷"));
    hots.add(new SortBean("r", "阮今天"));
    hots.add(new SortBean("r", "龚琳娜"));
    hots.add(new SortBean("", "苏醒"));
    hots.add(new SortBean("", "苏永康"));
    hots.add(new SortBean("t", "陶喆"));
    hots.add(new SortBean("s", "沙宝亮"));
    hots.add(new SortBean("s", "宋冬野"));
    hots.add(new SortBean("s", "宋伟"));
    hots.add(new SortBean("y", "袁成杰"));
    hots.add(new SortBean("y", "戚薇"));
    hots.add(new SortBean("q", "齐大友"));
    hots.add(new SortBean("q", "齐天大圣"));
    hots.add(new SortBean("p", "品冠"));
    hots.add(new SortBean("p", "吴克群"));
    hots.add(new SortBean("p", "BOBO"));
    hots.add(new SortBean("p", "Jobs"));
    hots.add(new SortBean("d", "动力火车"));
    hots.add(new SortBean("d", "伍佰"));
    hots.add(new SortBean("#", "#蔡依林"));
    hots.add(new SortBean("#", "$797835344$"));
    hots.add(new SortBean("j", "Jack"));
    hots.add(new SortBean("", "~夏先生"));
    return hots;
  }

  @Override
  public void onClick(Component component) {
    terminate();
  }
}
